package cc.magicjson.easy.batch.strategy;

import cc.magicjson.easy.batch.core.BatchStrategy;
import cc.magicjson.easy.batch.exception.BatchOperationException;
import cc.magicjson.easy.batch.model.BatchContext;
import cc.magicjson.easy.batch.model.BatchResult;

/**
 * 事务批处理策略
 */

public class TransactionalBatchStrategy<T> implements BatchStrategy<T> {
    @Override
    public BatchResult<T> execute(BatchContext<T> context) {
        return context.getTransactionTemplate().execute(status -> {
            try {
                return new SimpleBatchStrategy<T>().execute(context);
            } catch (Exception e) {
                status.setRollbackOnly();
                throw new BatchOperationException("Transaction failed", e);
            }
        });
    }
}
